package com.imedway.ihe.dtz.xds.dao;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLAssociation;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLClassification;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLExternalIdentifier;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLExtrinsicObject;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLInternationalString;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryObject;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryPackage;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLSlot;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.AvailabilityStatus;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.LocalizedString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.imedway.ihe.dtz.xds.dao.BaseDao;
import com.imedway.ihe.dtz.xds.entity.XdsAssociation;
import com.imedway.ihe.dtz.xds.entity.XdsClassification;
import com.imedway.ihe.dtz.xds.entity.XdsDescription;
import com.imedway.ihe.dtz.xds.entity.XdsExternalidentifier;
import com.imedway.ihe.dtz.xds.entity.XdsExtrinsicobject;
import com.imedway.ihe.dtz.xds.entity.XdsFileInfo;
import com.imedway.ihe.dtz.xds.entity.XdsName;
import com.imedway.ihe.dtz.xds.entity.XdsRegistrypackage;
import com.imedway.ihe.dtz.xds.entity.XdsSlot;

import cn.hutool.core.lang.UUID;
import lombok.extern.slf4j.Slf4j;

/**
 * @author dtz <br/>
 * @date 2024/7/4
 */
@Slf4j
@Repository
public class Iti42Dao{
	
	@Autowired
	private BaseDao baseDao;
	
	@Resource
	private JdbcTemplate jdbcTemplate;
		
	public void saveXdsFileInfo(List<XdsFileInfo> xdsFileInfoList)  {
		
		if(xdsFileInfoList!=null) {
			for(XdsFileInfo xdsFileInfo:xdsFileInfoList) {
				baseDao.saveXdsFileInfo(xdsFileInfo);
			}
		}
		
	}
	
	public void saveLocalizedStringList(List<LocalizedString> localizedStringList,String parent) {
		
		 for(LocalizedString l:localizedStringList) {
			 XdsDescription x=new XdsDescription();
			 x.setCharset(l.getCharset());
			 x.setLang(l.getLang());
			 x.setValue(l.getValue());
			 x.setParent(parent);
			 baseDao.saveXdsDescription(x);				 
		 }
	}
	
	public void saveNameLocalizedString(LocalizedString localizedString,String parent) {
		
		if(localizedString!=null) {
		     XdsName x=new XdsName();
			 x.setCharset(localizedString.getCharset());
			 x.setLang(localizedString.getLang());
			 x.setValue(localizedString.getValue());
			 x.setParent(parent);
			 baseDao.saveXdsName(x);	
		}
			 
	}
	
	public void saveEbXMLSlotList(List<EbXMLSlot> ebXMLSlotList,String parent) {
		int i=0;
		for(EbXMLSlot ebXMLSlot:ebXMLSlotList) {
			XdsSlot xdsSlot =new XdsSlot();
			xdsSlot.setParent(parent);
			xdsSlot.setName_(ebXMLSlot.getName());
			xdsSlot.setSequenceId(String.valueOf(i));
			List<String> stringList = ebXMLSlot.getValueList();
			if(stringList!=null&&stringList.size()==1) {
				xdsSlot.setValue(stringList.get(0));
			}else {
				String value = "";
				for(String s:stringList) {
					if(value.equals("")){
						value=s;
					}else {
						value=value+"^"+s;
					}
				}
			}
			i++;
			baseDao.saveXdsSlot(xdsSlot);
		}
	}
	
	public void saveEbXMLExternalIdentifierList(List<EbXMLExternalIdentifier> ebXMLExternalIdentifierList) {
		for(EbXMLExternalIdentifier ebXMLExternalIdentifier:ebXMLExternalIdentifierList) {
			XdsExternalidentifier xdsExternalidentifier = new XdsExternalidentifier();
			xdsExternalidentifier.setId(ebXMLExternalIdentifier.getId());
			xdsExternalidentifier.setLid(ebXMLExternalIdentifier.getId());
			xdsExternalidentifier.setIdentificationScheme(ebXMLExternalIdentifier.getIdentificationScheme());
			xdsExternalidentifier.setRegistryObject(ebXMLExternalIdentifier.getRegistryObject());
			xdsExternalidentifier.setValue(ebXMLExternalIdentifier.getValue());
			xdsExternalidentifier.setStatus(AvailabilityStatus.APPROVED.getQueryOpcode());
			xdsExternalidentifier.setObjectType(ebXMLExternalIdentifier.EXTERNAL_IDENTIFIER_OBJECT_TYPE);
			baseDao.saveXdsExternalidentifier(xdsExternalidentifier);
			EbXMLInternationalString ebXMLInternationalString = ebXMLExternalIdentifier.getName();
			List<LocalizedString> localizedStrings=ebXMLInternationalString.getLocalizedStrings();
			saveLocalizedStringList(localizedStrings,ebXMLExternalIdentifier.getId());
		}		
	}
	
	public void saveEbXMLExtrinsicObjectList(List<EbXMLExtrinsicObject> ebXMLExtrinsicObjectList) {
		
		for(EbXMLExtrinsicObject ebXMLExtrinsicObject:ebXMLExtrinsicObjectList) {
			XdsExtrinsicobject xdsExtrinsicobject = new XdsExtrinsicobject();
			xdsExtrinsicobject.setId(ebXMLExtrinsicObject.getId());
			xdsExtrinsicobject.setLid(ebXMLExtrinsicObject.getLid());
			xdsExtrinsicobject.setHome(ebXMLExtrinsicObject.getHome());
			xdsExtrinsicobject.setMimeType(ebXMLExtrinsicObject.getMimeType());
			xdsExtrinsicobject.setObjectType(ebXMLExtrinsicObject.getObjectType());
			if(ebXMLExtrinsicObject.getStatus()!=null) {
				xdsExtrinsicobject.setStatus(ebXMLExtrinsicObject.getStatus().getQueryOpcode());
				xdsExtrinsicobject.setIsOpaque("F");
			}
			xdsExtrinsicobject.setVersionName(ebXMLExtrinsicObject.getVersionInfo()!=null
					?ebXMLExtrinsicObject.getVersionInfo().getVersionName():null);
			xdsExtrinsicobject.setComment_(ebXMLExtrinsicObject.getVersionInfo()!=null
					?ebXMLExtrinsicObject.getVersionInfo().getComment():null);	
			
			baseDao.saveExtrinsicobject(xdsExtrinsicobject);
						
			List<LocalizedString> localizedStringList= new ArrayList<LocalizedString>();
			LocalizedString localizedS	=	ebXMLExtrinsicObject.getDescription();
			localizedStringList.add(localizedS);
			saveLocalizedStringList(localizedStringList,ebXMLExtrinsicObject.getId());
			 
			List<EbXMLSlot> ebXMLSlotList = ebXMLExtrinsicObject.getSlots();
			saveEbXMLSlotList(ebXMLSlotList,ebXMLExtrinsicObject.getId());
			
			List<EbXMLExternalIdentifier> ebXMLExternalIdentifierList = ebXMLExtrinsicObject.getExternalIdentifiers();
			saveEbXMLExternalIdentifierList(ebXMLExternalIdentifierList); 
			
			LocalizedString localizedString = ebXMLExtrinsicObject.getName();
			saveNameLocalizedString(localizedString,ebXMLExtrinsicObject.getId());
			
			List<EbXMLClassification> classificationsList = ebXMLExtrinsicObject.getClassifications();
			saveEbXMLClassificationList(classificationsList,ebXMLExtrinsicObject);
		
		}				
	}
	
	public void saveEbXMLRegistryPackageList(List<EbXMLRegistryPackage> ebXMLRegistryPackageList) {
		
		for(EbXMLRegistryPackage ebXMLRegistryPackage:ebXMLRegistryPackageList) {
			XdsRegistrypackage xdsRegistrypackage = new XdsRegistrypackage();
			xdsRegistrypackage.setId(ebXMLRegistryPackage.getId());
			xdsRegistrypackage.setLid(ebXMLRegistryPackage.getLid());
			xdsRegistrypackage.setHome(ebXMLRegistryPackage.getHome());
			xdsRegistrypackage.setObjectType(ebXMLRegistryPackage.getObjectType());
			if(ebXMLRegistryPackage.getStatus()!=null) {
				xdsRegistrypackage.setStatus(ebXMLRegistryPackage.getStatus().getQueryOpcode());	
			}
			xdsRegistrypackage.setVersionName(ebXMLRegistryPackage.getVersionInfo()!=null
					?ebXMLRegistryPackage.getVersionInfo().getVersionName():null);
			xdsRegistrypackage.setComment_(ebXMLRegistryPackage.getVersionInfo()!=null
					?ebXMLRegistryPackage.getVersionInfo().getComment():null);
			
			baseDao.saveRegistrypackage(xdsRegistrypackage);
			LocalizedString localizedStrings = ebXMLRegistryPackage.getDescription();			
			List<LocalizedString> localizedStringList=new ArrayList<LocalizedString>();
			localizedStringList.add(localizedStrings);
			saveLocalizedStringList(localizedStringList,ebXMLRegistryPackage.getId());
			 
			List<EbXMLSlot> ebXMLSlotList = ebXMLRegistryPackage.getSlots();
			saveEbXMLSlotList(ebXMLSlotList,ebXMLRegistryPackage.getId());
			
			List<EbXMLExternalIdentifier> ebXMLExternalIdentifierList = ebXMLRegistryPackage.getExternalIdentifiers();
			saveEbXMLExternalIdentifierList(ebXMLExternalIdentifierList); 
			
			LocalizedString localizedString = ebXMLRegistryPackage.getName();
			saveNameLocalizedString(localizedString,ebXMLRegistryPackage.getId());
			
			List<EbXMLClassification> classificationsList = ebXMLRegistryPackage.getClassifications();
			saveEbXMLClassificationList(classificationsList,ebXMLRegistryPackage);
			
			
		}
		
	}
	
	public void saveEbXMLAssociationList(List<EbXMLAssociation> ebXMLAssociationList) {
				
		for(EbXMLAssociation ebXMLAssociation:ebXMLAssociationList) {
			XdsAssociation xdsAssociation = new XdsAssociation();
			xdsAssociation.setId(ebXMLAssociation.getId());
			xdsAssociation.setLid(ebXMLAssociation.getLid());
			xdsAssociation.setHome(ebXMLAssociation.getHome());
			xdsAssociation.setObjectType(ebXMLAssociation.getObjectType());
			if(ebXMLAssociation.getStatus()!=null) {
				xdsAssociation.setStatus(ebXMLAssociation.getStatus().getQueryOpcode());
			}
			xdsAssociation.setVersionName(ebXMLAssociation.getVersionInfo()!=null
					?ebXMLAssociation.getVersionInfo().getVersionName():null);
			xdsAssociation.setComment_(ebXMLAssociation.getVersionInfo()!=null
					?ebXMLAssociation.getVersionInfo().getComment():null);
			xdsAssociation.setAssociationType(ebXMLAssociation.getAssociationType().getOpcode30());
			xdsAssociation.setSourceObject(xdsAssociation.getSourceObject());
			xdsAssociation.setTargetObject(xdsAssociation.getTargetObject());
			baseDao.saveAssociation(xdsAssociation);
			
			List<EbXMLSlot> ebXMLSlotList = ebXMLAssociation.getSlots();
			saveEbXMLSlotList(ebXMLSlotList,ebXMLAssociation.getId());
						
		}
		
	}
	
	public void saveEbXMLClassificationList(List<EbXMLClassification> ebXMLClassificationList,EbXMLRegistryObject ebXMLRegistryObject) {
		
		for(EbXMLClassification ebXMLClassification:ebXMLClassificationList) {
						
			XdsClassification x = new XdsClassification();
			String lid = UUID.randomUUID().toString();
			if(ebXMLRegistryObject!=null) {
				lid=ebXMLRegistryObject.getLid()!=null?ebXMLRegistryObject.getLid():lid;
			}
			x.setId(lid);		
			x.setLid(lid);
			x.setObjectType(ebXMLClassification.CLASSIFICATION_OBJECT_TYPE);
			x.setStatus(AvailabilityStatus.APPROVED.getQueryOpcode());			
			x.setClassifiedObject(ebXMLClassification.getClassifiedObject());
			
			x.setClassificationNode(ebXMLClassification.getClassificationNode());
			x.setClassificationScheme(ebXMLClassification.getClassificationScheme());

			
			List<EbXMLSlot> ebXMLSlots = ebXMLClassification.getSlots();
			saveEbXMLSlotList(ebXMLSlots,lid);
			
			LocalizedString localizedString = ebXMLClassification.getName();
			saveNameLocalizedString(localizedString,lid);
			
			EbXMLInternationalString ebXMLInternationalString=ebXMLClassification.getNameAsInternationalString();
			if(ebXMLInternationalString!=null) {
				saveLocalizedStringList(ebXMLInternationalString.getLocalizedStrings(),lid);
			}
		}	
	}
		
	
}
